package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryCitizenCardRes;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/28 14:35
 * description:
 */
public class AppCardAdapter extends RecyclerView.Adapter<AppCardAdapter.AppCardHolder> {

    private Context mContext;
    private ArrayList<QueryCitizenCardRes.CitizenCardInfoListBean> viewData;
    private List<QueryCitizenCardRes.CitizenCardInfoListBean.FunctionListBean> functionList;
    private List<QueryCitizenCardRes.CitizenCardInfoListBean.FunctionListBean> mList = new ArrayList<>();

    public AppCardAdapter(Context context) {
        mContext = context;
    }

    @Override
    public AppCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.appcard_card_view, parent, false);
        return new AppCardHolder(view);
    }

    @Override
    public void onBindViewHolder(AppCardHolder holder, int position) {


        LinearLayoutManager mmanager = new LinearLayoutManager(mContext);
        mmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.appcardRv.setLayoutManager(mmanager);
        AppcardItemAdapter itemAdapter = new AppcardItemAdapter(mContext);
        holder.appcardRv.setAdapter(itemAdapter);
        if (viewData != null) {
            QueryCitizenCardRes.CitizenCardInfoListBean listBean = viewData.get(position);
            holder.cardId.setText(listBean.getBindCardNo());
            holder.cardName.setText("持卡人：" + listBean.getCustName());
            holder.cardType.setText(listBean.getCardTypeName());
            functionList = listBean.getFunctionList();
            mList.clear();
            for (int i = 0; i < functionList.size(); i++) {
                QueryCitizenCardRes.CitizenCardInfoListBean.FunctionListBean bean = functionList.get(i);
                String stateName = bean.getStateName();
                if (stateName.equals("已开通")) {
                    mList.add(bean);
                }
            }
            itemAdapter.setViewData(mList);
            itemAdapter.notifyDataSetChanged();


        }

//        AppCardAdapter appCardAdapter = new AppCardAdapter(mContext);
//        holder.appcardRv.setAdapter(appCardAdapter);
    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
    }

    public void setViewData(ArrayList<QueryCitizenCardRes.CitizenCardInfoListBean> viewData) {
        this.viewData = viewData;
    }


    public static class AppCardHolder extends RecyclerView.ViewHolder {

        private final RecyclerView appcardRv;
        private final TextView cardType;
        private final TextView cardName;
        private final TextView cardId;
        private final ImageView cardIv;
        private final CardView cardCv;

        public AppCardHolder(View itemView) {
            super(itemView);
            appcardRv = (RecyclerView) itemView.findViewById(R.id.appcard_rv);
            cardType = (TextView) itemView.findViewById(R.id.appcard_cardtype);
            cardName = (TextView) itemView.findViewById(R.id.appcard_username);
            cardId = (TextView) itemView.findViewById(R.id.appcard_cardid);
            cardIv = (ImageView) itemView.findViewById(R.id.card_iv);
            cardCv = (CardView) itemView.findViewById(R.id.card_cv);
        }
    }

}
